package com.servlet.response; /**
 * @author wzm
 * @create 2023-11-06 11:26
 * project_name:web2310
 * @Description
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/resp3")
public class ResponseServlet3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);//调用doPost
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //当访问当前Servlet的时候 设置响应一个普通的字符响应体
        //解决响应数据乱码
        //方式一
          //设置服务器的字符集为UTF-8
            //response.setCharacterEncoding("UTF-8");
          //通过响应头告诉浏览器也要以UTF-8的编码对当前的内容进行解析
            //response.setHeader("Content-Type","text/html;charset=UTF-8");

        //方式二 同时设置服务器和客户端都使用UTF-8的编码且同时设置响应头
        response.setContentType("text/html;charset=UTF-8");//推荐


        //1.获取字符输出流
        PrintWriter writer = response.getWriter();
        //2.利用输出流输出字符到响应体中
        //writer.write("aaa");//输出简单的aaa字符
       // writer.write("好好学习");

        //响应网页
        //writer.write("<h1>我是网页</h1>");
//        writer.write("<!DOCTYPE html>");
//        writer.write("<html lang=\"en\">");
        //...
        writer.write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
                "    <title>Document</title>\n" +
                "    <style>\n" +
                "        .box{\n" +
                "            width: 100px;\n" +
                "            height: 100px;\n" +
                "            background-color: red;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "\n" +
                "    <div class=\"box\">啦啦</div>\n" +
                "    \n" +
                "</body>\n" +
                "</html>");
        //原生的Servlet响应网页到浏览器上及其复杂 思路：跳转到你要展示的html 使用html可以显示但是又无法动态更新网页内容，后期使用jsp替代html
    }
}
